package com.evo.chat.web.aspect;

import com.evo.chat.common.ResultEnum;
import com.evo.chat.common.exception.ChatException;
import com.evo.chat.common.utils.RedisUtil;
import com.evo.chat.common.utils.ResultUtil;
import com.evo.chat.web.domain.UserInfo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

//@Aspect
//@Component
public class HttpAspect {

//    private Logger logger = LoggerFactory.getLogger(HttpAspect.class);
//
//    @Resource
//    RedisUtil redisUtil;
//
//    @Pointcut("execution(public * com.evo.chat.web.controller.WordsController.*(..))")
//    public void words () {
//    }
//
//    @Before("words()")
//    public void doBefore () {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//
//        String authorization = request.getHeader("Authorization");
//        System.out.println("token => " + authorization);
//
//        if(StringUtils.isEmpty(authorization)){
//            throw new ChatException(ResultEnum.USER_NOT_LOGIN);
//        }
//        UserInfo userModel = (UserInfo) redisUtil.get(authorization);
//        if(userModel == null){
//            throw new ChatException(ResultEnum.USER_NOT_LOGIN);
//        }
//
////        // url
////        logger.info("url={}", request.getRequestURL());
////
////        // method
////        logger.info("method={}", request.getMethod());
////
////        // ip
////        logger.info("ip={}", request.getRemoteAddr());
////
////        // 类方法
////        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
////
////        // 参数
////        logger.info("args={}", joinPoint.getArgs());
//    }
//
//    @After("words()")
//    public void doAfter () {
//        logger.info("22222");
//    }
//
//    @AfterReturning(returning = "object", pointcut = "words()")
//    public void doAfterReturning (Object object) {
//        logger.info("response={}", object.toString());
//    }
}
